package wang.wangby.lazydoc.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.Data;
import wang.wangby.annotation.Api;
import wang.wangby.annotation.Remark;
import wang.wangby.lazydoc.model.ApiInfo;
import wang.wangby.lazydoc.service.LazyDocService;
import wang.wangby.web.BaseController;

@RequestMapping("${lazydoc.webroot:/lazydoc}")
@EnableConfigurationProperties
public class LazyDocController extends BaseController{

	@Autowired
	private LazyDocService service;
	
	@RequestMapping("/index")
	@ResponseBody
	public String index() {
		return $("/lazydoc/index");
	}
	
	
	@RequestMapping(value = "/detail/{index}", method = RequestMethod.GET)
	@ResponseBody
	public String detail(@PathVariable Integer index) {
		List<ApiInfo> apis = service.getApiInfo();
		return $("/lazydoc/detail",apis.get(index));
	}
	
	
	@Api(name="菜单树显示的内容",path="/treeView")
	public List<TreeView> treeView() {
		List<TreeView> list = new ArrayList();
		TreeView parent = new TreeView();
		parent.setNodes(new ArrayList());
		list.add(parent);
		List<ApiInfo> apis = service.getApiInfo();
		int id = 0;
		//按接口所在class归类,
		for (ApiInfo info : apis) {
			String clssName=info.getClazz().getSimpleName();
			String methodName=info.getMethod().getName();
			
			if (parent.text == null) {
				parent.text =clssName;
			}
			if (!parent.text.equals(clssName)) {
				parent = new TreeView();
				parent.text = clssName;
				parent.nodes = new ArrayList();
				list.add(parent);
			}

			TreeView children = new TreeView();
			children.text = methodName;
			children.id = id + "";
			parent.nodes.add(children);
			id++;
		}
		return list;
	}


	@Data
	@Remark("bootstrap.treeView显示所需对象")
	public class TreeView {
		@Remark("节点唯一标识")
		private String id;
		@Remark("下级节点")
		private List<TreeView> nodes;
		@Remark("显示文本")
		private String text;
	}
	
}
